package com.lhj.library.application.config;

import cn.dev33.satoken.stp.StpInterface;
import com.lhj.library.infra.basic.entity.Role;
import com.lhj.library.infra.basic.entity.UserRole;
import com.lhj.library.infra.basic.service.RoleService;
import com.lhj.library.infra.basic.service.UserRoleService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 自定义权限加载接口实现类
 */
@Component    // 保证此类被 SpringBoot 扫描，完成 Sa-Token 的自定义权限验证扩展
public class StpInterfaceImpl implements StpInterface {

    @Resource
    private UserRoleService userRoleService;

    @Resource
    private RoleService roleService;

    /**
     * 返回一个账号所拥有的权限码集合 
     */
    @Override
    public List<String> getPermissionList(Object loginId, String loginType) {
        return Collections.emptyList();
    }

    /**
     * 返回一个账号所拥有的角色标识集合 (权限与角色可分开校验)
     */
    @Override
    public List<String> getRoleList(Object loginId, String loginType) {
        UserRole userRole = userRoleService.queryById(Long.valueOf((String) loginId));
        Long roleId = userRole.getRoleId();
        Role role = roleService.queryById(roleId);
        List<String> list = new ArrayList<String>();
        list.add(role.getRoleName());
        return list;
    }

}
